package com.ruoyi.common.minio;

import cn.hutool.core.util.IdUtil;
import io.minio.MinioClient;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @author Licl
 *
 * docker 启动命令
 * docker run -it -p 9000:9000 --name minio \
 * -d --restart=always \
 * -e "MINIO_ACCESS_KEY=admin" \
 * -e "MINIO_SECRET_KEY=admin123456" \
 * -v /mnt/minio/data:/data \
 * -v /mnt/minio/config:/root/.minio \
 * minio/minio server /data
 *
 * Linux 服务器
 * MinIoConfig.getEndpoint()：http://115.239.209.233:9999
 * MinIoConfig.getAccessKey()：jiuling
 * MinIoConfig.getSecretKey()：4606B061843A467495A73A57A5F6A1C5
 *
 * MINIO_ACCESS_KEY=jiuling MINIO_SECRET_KEY=4606B061843A467495A73A57A5F6A1C5 nohup /root/MinIO/minio  server --address 0.0.0.0:9999 /root/MinIO/data  > /root/MinIO/minio.log 2>&1 &
 *
 *
 */
public class MinIOUtils {

    public static final String MIN_IO_LOCAL_PATH = MinIoConfig.getFilePrefix() + "/" + MinIoConfig.getKeyPrefix();

    public static void putObject(String objectName, InputStream stream, String contentType, Long size) throws Exception {
        MinioClient minioClient = new MinioClient(MinIoConfig.getEndpoint(), MinIoConfig.getAccessKey(), MinIoConfig.getSecretKey());
        minioClient.putObject(MinIoConfig.getBucketName(), MinIoConfig.getKeyPrefix() + objectName, stream, size, contentType);
    }

    public static void putObject(String ossKey, MultipartFile multipartFile) throws Exception {
        MinioClient minioClient = new MinioClient(MinIoConfig.getEndpoint(), MinIoConfig.getAccessKey(), MinIoConfig.getSecretKey());
        InputStream inputStream = multipartFile.getInputStream();
        minioClient.putObject(MinIoConfig.getBucketName(), MinIoConfig.getKeyPrefix() + ossKey, inputStream, multipartFile.getSize(), multipartFile.getContentType());
        inputStream.close();
    }

    public static String putObjectRetPath(MultipartFile multipartFile) throws Exception {
        String ossKey = IdUtil.simpleUUID();
        MinioClient minioClient = new MinioClient(MinIoConfig.getEndpoint(), MinIoConfig.getAccessKey(), MinIoConfig.getSecretKey());
        minioClient.putObject(MinIoConfig.getBucketName(), MinIoConfig.getKeyPrefix() + ossKey, multipartFile.getInputStream(), multipartFile.getSize(), multipartFile.getContentType());
        return MinIoConfig.getEndpoint() + "/" + MinIoConfig.getBucketName() + "/" + MinIoConfig.getKeyPrefix() + ossKey;
    }

}
